package network

import (
	"fmt"

	"gitee.com/liumou_site/gcs"
	"gitee.com/liumou_site/logger"
)

// AddDns 为指定的网络连接配置DNS服务器地址。
func (c *NmcliConnectionStruct) AddDns() (err error) {
	shell := gcs.NewShell()
	fmt.Println("增加DNS")
	if len(c.Dns) >= 1 {
		for index, server := range c.Dns {
			if index == 1 {
				cmd := fmt.Sprintf("nmcli connection modify %s ipv4.Dns %s", c.uuid, server)
				shell.RunShell(cmd)
				return shell.Err
			} else {
				cmd := fmt.Sprintf("nmcli connection modify %s +ipv4.Dns %s", c.uuid, server)
				shell.RunShell(cmd)
				return shell.Err
			}
		}
	} else {
		logger.Error("当前实例未配置DNS服务器信息,请先把DNS信息赋值到结构体中")
		err = fmt.Errorf("dns服务器列表为空")
		return err
	}
	return nil
}
